from fastapi import APIRouter
from fastapi import Request
from fastapi.templating import Jinja2Templates
from models import *

student_api = APIRouter()


@student_api.get("/")
async def getAllStudent(request: Request):
    templates = Jinja2Templates(directory="templates")
    ret_data = await MedicineInfo.filter().limit(10)  # [Student(),Student(),...]
    print(ret_data[0].year)
    return templates.TemplateResponse(
        'index.html',
        {
            'request': request,  # 注意，返回模板响应时，必须有request键值对，且值为Request请求对象
            'itemData': ret_data,
        }
    )


@student_api.get('/get_content')
async def getAllStudent(request: Request):
    templates = Jinja2Templates(directory="templates")
    ret_data = await MedicineInfo.filter().limit(10)  # [Student(),Student(),...]
    print(ret_data[0].year)
    return templates.TemplateResponse(
        'index.html',
        {
            'request': request,  # 注意，返回模板响应时，必须有request键值对，且值为Request请求对象
            'itemData': ret_data,
        }
    )


@student_api.get('/detail/{p}')
async def getDetails(request: Request, p):
    templates = Jinja2Templates(directory="templates")
    ret_data = await MedicineDetail.filter(s_id=p).all()  # [Student(),Student(),...]
    print(ret_data)
    return templates.TemplateResponse(
        'details.html',
        {
            'request': request,  # 注意，返回模板响应时，必须有request键值对，且值为Request请求对象
            'itemData': ret_data,
        }
    )
